<?php
/**
 * 获取用户角色权限
 * User: Administrator
 * Date: 2018\9\12 0012
 * Time: 16:53
 */

namespace app\Lib\Model\AdminAuth\GetAuthorization;


use app\Lib\InterfaceIn\AdminAuth\GetAuthorization\GetRoleAuthHandle;
use app\Lib\InterfaceIn\Role\RoleHandle;
use app\Lib\Model\AdminAuth\MenuAuth;
use app\Lib\Model\DbModel\AdminMenu;
use app\Lib\Model\DbModel\AdminRoleAuth;

class GetRoleAuth implements GetRoleAuthHandle
{
    /** @var RoleHandle $role */
    private $role;
    private $cache;
    private $cacheName;
    private $cacheNameTow;
    private $menuAuthUpdateCache = 'menuAuthUpdateTime';
    private $table;
    /**
     * GetRoleAuth constructor.
     * @param RoleHandle $role
     * @param bool $cache
     */
    public function __construct(RoleHandle $role, $cache = true)
    {
        $this->role  = $role;
        $this->cache = $cache;
        $this->cacheName = 'roleAuth_role_id_'.$this->role->getRoleId();
        $this->cacheNameTow ='roleAuth_role_id_'.$this->role->getRoleId().'_updateTime';
    }

    /**
     * 执行
     * @return array
     */
    public function run()
    {
        return $this->getRoleAuthObj();
    }

    /**
     * 删除缓存
     * @return bool
     */
    public function delCache()
    {
       cache($this->cacheName,null);
       cache($this->cacheNameTow,null);
       return true;
    }

    /**
     * 获取用户权限对象
     * @return array
     */
    private function getRoleAuthObj(){
        $AuthObj = [];
        $roleAuthUpdateTime = cache($this->cacheNameTow);
        $menuAuthUpdateTime = cache($this->menuAuthUpdateCache);
        $cacheStatus = $this->cache;
        /*当菜单权限缓存更新时间>角色权限缓存更新时间删除缓存*/
        if($menuAuthUpdateTime>$roleAuthUpdateTime){
            $this->delCache();
            $cacheStatus = false;
        }
        /*获取权限id*/
        if($cacheStatus){
            $result =  $this->getCache();
        }else{
            $result =  $this->getRoleAuth();
            cache($this->cacheName,json_encode($result));
            cache($this->cacheNameTow,time());
        }
        /*生成权限对象*/
        if(is_array($result) && !empty($result)){
            foreach ($result as $v){
                $AuthObj[]= new MenuAuth($v);
            }
        }
        return $AuthObj;
    }

    /**
     * 获取用户个人权限缓存
     * @return array
     */
    private function getCache(){
        $data = cache($this->cacheName);
        if($data){
            $data = json_decode($data,true);
        }else{
            $data = $this->getRoleAuth();
            cache($this->cacheName,json_encode($data));
            cache($this->cacheNameTow,time());
        }
        return $data;
    }

    /**
     * 获取用户个人权限执行
     * @return array
     */
    private function getRoleAuth(){
        /*超级管理员拥有一切权限*/
        if($this->role->isSuperAdministrator()){
            $model  = ((new AdminMenu()));
            $result = $model->column($model->_id);
        }else{
            /*其他角色查询权限*/
            $model  = $this->getTable();
            $map    = [$model->_role_id=>$this->role->getRoleId()];
            $result = $model->where($map)->column($model->_menu_id);
        }
        return $result;
    }

    /**
     * 获取数据库
     * @return AdminRoleAuth
     */
    private function getTable(){
        if(empty($this->table)){
            $this->table = new AdminRoleAuth();
        }
        return $this->table;
    }

}